/*
 * 户口信息
 */
PersonCurd = Ext.extend(RockyGridCurd, {
	baseUrl : 'person.action',
	winCfg : {
		maximizable : true,
		width : 510,
		title : "详细信息"
	},
	title : "户口信息管理",
	trackMouseOver : false,
	loadMask : false,
	autoWidth : false,
	autoScroll : true,
	closable : true,
	viewConfig : {
		forceFit : false
	},
	constructor : function(config) {
		this.areatree = new Ext.tree.TreePanel({
			width : 560,
			height : 500,
			border : false,
			rootVisible : false,
			autoScroll : true,
			root : new Ext.tree.AsyncTreeNode({
				id : "-1",
				text : "未归类",
				expanded : true,
				loader : new RockyCurd.TreeLoader({
					dataUrl : "genAreaTree.action",
					expanded : true,
					nodeMeta : {
						id : 'id',
						text : 'showname'
					}
				})
			}),
			listeners : {
				click : function(node, e) {
					if( !node.isLeaf()){
					 	Ext.Msg.alert("提示","请选择行政村下的自然村！");
					 	return false;
					}
					var combo = this.fp.find('name', 'area')[0];
					var hidden = this.fp.find('name', 'areaid')[0];
					combo.setValue(node.text);
					hidden.setValue(node.id);
					this.fp.find('name', 'address')[0].setValue(node.attributes.areapath);
					this.areapopwin.hide();
					return true;
				},
				scope : this
			}
		});
		this.store = new Ext.data.JsonStore({
			id : "id",
			url : 'person.action?cmd=read',
			root : "data",
			totalProperty : "rowCount",
			remoteSort : false,
			fields : ["areaself","area_code","familystatus", "familyid", "familycode", "area", "areaid", "address", "householder", "householderRelation", "name", "sex", "birthday", "cardnumber", "residenceType", "id"],
			listeners : {
				beforeload : function() {
					this.store.baseParams.limit = 20;
					var keywordFilterValue = this.keywordFilter.getValue();
					this.store.baseParams.keywordFilterValue = keywordFilterValue || '';
				},
				scope : this
			}
		});
		this.keywordFilter = new Ext.form.TextField({
			width : 100,
			pressed : true,
			scope : this
		});
		Ext.apply(this, config);
		var sm = new Ext.grid.CheckboxSelectionModel();
		var cmpId = this.id;
		this.cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), sm, {
//			header : "id_person",
//			sortable : true,
//			width : 80,
//			dataIndex : "id"
//		}, {
//			header : "familyid",
//			sortable : true,
//			width : 80,
//			dataIndex : "familyid"
//		}, {
			header : "姓名",
			sortable : true,
			width : 100,
			dataIndex : "name"
		}, {
			header : "性别",
			sortable : true,
			width : 50,
			dataIndex : "sex",
			renderer : function(v) {
				return selectStore(Sex_Store, v);
				/*switch (v){
					case '1' :return '男';
					case '0' :return '女'*/
			}
		}, {
			header : "出生年月",
			sortable : true,
			width : 100,
			dataIndex : "birthday"
		}, {
			header : "身份证号",
			sortable : true,
			width : 200,
			dataIndex : "cardnumber"
		}, {
			header : "户主关系",
			sortable : true,
			width : 60,
			dataIndex : "householderRelation",
			renderer : function(v) {
				return selectStore(Relation_Store, v);
			}
		}, {
			header : "户主姓名",
			sortable : true,
			width : 80,
			dataIndex : "householder"
		}, {
			header : "户籍类型",
			sortable : true,
			width : 60,
			dataIndex : "residenceType",
			renderer : function(v) {
				return selectStore(Residence_Store, v);
			}
		}, {
			header : "家庭户籍状态",
			sortable : true,
			width : 100,
			renderer : function(v) {
				if (v != "false") {
					return "<font color=red>删除</font>";
				} else {
					return "<font color=blue>正常</font>";
				}
			},
			dataIndex : "familystatus"
		}, {
			header : "家庭编码",
			sortable : true,
			width : 100,
			dataIndex : "familycode"
		}, {
			header : "家庭所在自然村编码",
			sortable : true,
			width : 150,
			dataIndex : "area_code"			
		}, {
			header : "家庭所在自然村",
			sortable : true,
			width : 150,
			dataIndex : "area"	
		}, {
			header : "住址",
			sortable : true,
			width : 300,
			dataIndex : "address"
		}, {
			header : "所属自然村",
			sortable : true,
			width : 500,
			renderer : function(v,el,r) {
				if (!v) {
					return r.data.area;
				} 
				if(v!=r.data.area){
					return String.format("<font color=red>属于自然村'{0}'，和家庭所属自然村'{1}'不一致",v,r.data.area);
				}else{
					return v;
				}
			},			
			dataIndex : "areaself"					
		}]);
		this.cm.defaultSortable = true;
		Ext.apply(this, {
			sm : sm,
			cm : this.cm,
			store : this.store,
			tbar : ['   ', {
				text : '添加',
				iconCls : 'add',
				pressed : false,
				handler : this.create,
				scope : this
			}, '   ', {
				text : '修改',
				iconCls : 'edit',
				pressed : false,
				handler : this.edit,
				scope : this
			}, '   ', {
				text : '删除',
				iconCls : 'remove',
				pressed : false,
				handler : this.removeData,
				scope : this
			}, '   ', {
				text : '刷新',
				iconCls : 'refresh',
				pressed : false,
				handler : this.refresh,
				scope : this
			}, new Ext.Toolbar.Fill(), '请输入身份证号码: ', this.keywordFilter, {
				text : '查询',
				iconCls : 'search',
				handler : this.search,
				scope : this
			}, {
				text : '重置',
				iconCls : 'cancel',
				pressed : true,
				handler : this.onReset,
				scope : this
			}, '   '],
			bbar : new Ext.PagingToolbar({
				pageSize : 20,
				store : this.store,
				displayInfo : true,
				displayMsg : '显示{0}-{1}，共{2}条记录',
				emptyMsg : "没有可显示的记录！"
			})
		});
		PersonCurd.superclass.constructor.call(this, config);
	},
	onReset : function() {
		this.keywordFilter.setValue("");
	},
	save : function() {
		if (this.fp.form.isValid()) {
			var householderRelation = this.fp.find('name', 'householderRelation')[0];
			/*
			if(householderRelation.getValue()==0){
			 	var familycode = this.fp.find('name','familycode')[0];
				if(Ext.isEmpty( familycode.getValue() )){
					Ext.Msg.alert("提示","户主则必须填写家庭编码！");
					familycode.focus(true);
					return false;
				}
			}
			 */
			var id = this.fp.find('name', "id")[0].getValue();
			var formData = this.fp.form.getValues();
			Ext.Ajax.request({
				url : this.baseUrl,
				params : {
					'jsondata' : Ext.encode(formData),
					'cmd' : (id ? "update" : "create"),
					'from' : 'action'
				},
				method : 'POST',
				success : function(response) {
					Ext.getBody().unmask();
					var r = Ext.decode(response.responseText);
					if (!r.success) {
						Ext.Msg.alert("提示信息", "失败，由以下原因所致：<br/>" + (r.errors ? r.errors : "未知原因"));
					} else {
						Ext.Msg.alert("提示信息", "保存成功!");
						this.closeWin();
						this.refresh();
					}
				},
				failure : function(Optional) {
					Ext.getBody().unmask();
					Ext.Msg.alert("提示信息", "连接服务器失败!");
					this.closeWin();
				},
				scope : this
			});

		}
	},
	createPopwin : function() {
		if (!this.popwin) {
			this.popwin = new Ext.Window({
				autoScroll : true,
				width : 616,
				height : 400,
				title : '请选择家庭',
				buttonAlign : "center",
				closeAction : "hide",
				modal : true,
				shadow : true,
				items : this.familygrid,
				buttons : [{
					text : "确定",
					handler : function() {
						var record = this.familygrid.getSelectionModel().getSelected();
						if (!record) {
							Ext.Msg.alert("提示", "请选择所属家庭!");
							return;
						}
						this.popwin.hide();
						this.fp.find('name', 'area')[0].setValue(record.data.area);
						this.fp.find('name', 'areaid')[0].setValue(record.data.areaid);
						this.fp.find('name', 'householder')[0].setValue(record.data.householdernameR);
						this.fp.find('name', 'familycode')[0].setValue(record.data.familycode);
						this.fp.find('name', 'familyid')[0].setValue(record.data.id);

					},
					scope : this
				}, {
					text : "关闭",
					handler : function() {
						this.popwin.hide();
					},
					scope : this
				}]
			});
		}
		this.popwin.show(this);
		this.familygrid.refresh();
	},
	createAreaPopwin : function() {
		if (!this.areapopwin) {
			this.areapopwin = new Ext.Window({
				width : 573,
				height : 545,
				autoScroll : false,
				title : '请选择所在自然村',
				buttonAlign : "center",
				closeAction : "hide",
				modal : true,
				shadow : true,
				items : this.areatree
			});
		}
		this.areapopwin.show(null,function(){
			if (this.areatree.rendered) {
				this.areatree.root.reload();
				this.areatree.root.expand(true);
			}		
		},this);
	},
	createFamilygrid : function() {
		if (!this.familygrid) {
			this.familygrid = new FamilyCurd({
				header : false,
				id : 'FamilyCurdSearch',
				width : 588,
				height : 322,
				afterRender : function() {
					FamilyCurd.superclass.afterRender.call(this);
					this.getTopToolbar().items.item(0).setVisible(false);
					this.getTopToolbar().items.item(1).setVisible(false);
					this.getTopToolbar().items.item(2).setVisible(false);
				}
			})
		}
	},
	createForm : function() {
		var formPanel = new Ext.form.FormPanel({
			autoScroll : true,
			frame : true,
			border : false,
			bodyStyle : 'position:relative;margin:15px 0 0px 15px',
			labelAlign : 'left',
			width : 500,
			height : 300,
			defaults : {
				width : 450
			},
			items : [{
				xtype : "hidden",
				name : "id"
			}, {
				xtype : "hidden",
				name : "areaid"
			}, {
				xtype : "hidden",
				name : "familyid"
			}, {
				xtype : 'textfield',
				fieldLabel : '姓名*',
				allowBlank : false,
				name : 'name',
				anchor : '95%'
			}, {
				xtype : "combo",
				fieldLabel : "性别",
				blankText : '必选项，请选择',
				emptyText : '请选择性别',
				editable : false,
				triggerAction : 'all',
				anchor : '95%',
				valueField : "key",
				displayField : "value",
				mode : 'local',
				store : Sex_Store,
				name : "sex",
				hiddenName : "sex"
			}, {
				xtype : 'datefield',
				format : 'Y-m-d',
				editable : true,
				fieldLabel : '出生年月*',
				allowBlank : false,
				readOnly : true,
				name : 'birthday',
				anchor : '95%'
			}, {
				xtype : 'textfield',
				fieldLabel : '身份证号*',
				allowBlank : false,
				name : 'cardnumber',
				anchor : '95%'
			}, {

				xtype : "combo",
				fieldLabel : "户籍类型*",
				blankText : '必选项，请选择',
				emptyText : '请选择',
				editable : false,
				triggerAction : 'all',
				anchor : '95%',
				valueField : "key",
				displayField : "value",
				mode : 'local',
				store : Residence_Store,
				name : "residenceType",
				hiddenName : "residenceType"
			}, {
				xtype : 'combo',
				fieldLabel : '与户主关系*',
				allowBlank : false,
				editable : false,
				triggerAction : 'all',
				emptyText : '请选择',
				anchor : '95%',
				valueField : "key",
				displayField : "value",
				mode : 'local',
				store : Relation_Store,
				name : "householderRelation",
				listeners : {
					change : function(field, newValue, oldValue) {
						if (newValue != oldValue && newValue == 0) {
							var householder = this.fp.find('name', 'householder')[0];
							householder.setValue(this.fp.find('name', 'name')[0].getValue());
						}
						return true;
					},
					scope : this
				},
				hiddenName : "householderRelation"
			}, {
				xtype : 'trigger',
				fieldLabel : '家庭编码',
				allowBlank : false,
				editable : true,
				name : 'familycode',
				onTriggerClick : function(e) {
					this.createFamilygrid();
					this.familygrid.keywordFilter.setValue(this.fp.find('name', 'familycode')[0].getValue());
					this.createPopwin();
				}.createDelegate(this),
				anchor : '95%'
			}, {
				xtype : 'trigger',
				fieldLabel : '户主姓名*',
				allowBlank : false,
				editable : true,
				name : 'householder',
				onTriggerClick : function(e) {
					this.createFamilygrid();
					this.familygrid.keywordFilter.setValue(this.fp.find('name', 'householder')[0].getValue());
					this.createPopwin();
				}.createDelegate(this),
				anchor : '95%'
			}, {
				xtype : "trigger",
				fieldLabel : "所属地区",
				allowBlank : false,
				emptyText : '请选择',

				name : "area",
				anchor : '95%',
				editable : false,
				readOnly : true,
				onTriggerClick : function(e) {
					this.createAreaPopwin();
				}.createDelegate(this)
			}, {
				xtype : 'textfield',
				fieldLabel : '住址',

				name : 'address',
				anchor : '95%'

			}]
		});
		return formPanel;
	}
});